//
//  AppUrl.swift
//  B2BAutoMall
//
//  Created by heew on 16/1/28.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit


let __kRequest_params_time            = "time"
let __kRequest_params_timeCheckValue  = "timeCheckValue"
let __kRequest_params_token           = "token"
let __kRequest_params_tokenCheckValue = "tokenCheckValue"
let __kRequest_params_ctype           = "ctype"
let __kRequest_params_sourceType      = "sourceType"
let __kRequest_params_project_id      = "projectId"
let __kTime                           = "7cf1f0263ef39091dc48604aac8c8f9a"
let __kToken                          = "d0468866ee36c1995563e8f8c6063a14"
let __projectId                       = "1026"
let __ctype                           = "1"

open class YYURL: NSObject {
    var action : String
    var params : String
    var dictImages : NSMutableDictionary?
    
    init(action: String, params: String) {
        self.action = action
        self.params = params
        super.init()
    }
    
    class func url(action : String,params : String) -> YYURL
    {
        let url = YYURL(action: action, params: params)
        let timeString = NSString(format: "%.f", NSDate().timeIntervalSince1970) as NSString
        let timeCheckValue = timeString.appending(__kTime).md5 as String
        url.paramsAddValue(value: timeString as String, key: __kRequest_params_time)
        url.paramsAddValue(value: timeCheckValue, key: __kRequest_params_timeCheckValue)
        url.paramsAddValue(value: "1", key: __kRequest_params_sourceType)
        url.paramsAddValue(value: __projectId, key: __kRequest_params_project_id)
        url.autoAddTokenParam()
        return url
    }
    
    class func checkVersionUrlWithAction(action : String,params : String) -> YYURL
    {
        return YYURL(action: action, params: params)
    }
    
    func paramsAddValue(value:String,key:String)
    {
        if params.contains(key + "=") { return }
        return params = params+"&"+key+"="+value
    }
    
    func autoAddTokenParam()
    {
        let token = BaseUser.shareUser().getToken()

        let tokenCheckValue = token.appendingFormat(__kToken).md5
        paramsAddValue(value: token, key: __kRequest_params_token)
        paramsAddValue(value: tokenCheckValue as String, key: __kRequest_params_tokenCheckValue)
    }
    
    override open var description : String
    {
        return self.joinActionAndParams()
    }
    
    func joinActionAndParams() -> String
    {
        if params.isEmpty { return action}
        var join = "&"
        if !action.contains("?") {join = "?"}
        return action + join + params
    }
    
    func addImageWithKey(key: NSString , image: UIImage) {
        if dictImages == nil {
            self.dictImages = NSMutableDictionary()
        }
        self.dictImages?.setObject(image, forKey: key)
    }
    
    var queryDictionary : [String : String] {
        let queryArray = self.params.components(separatedBy: "&")
        var dic = ["":""]
        for queryStr in queryArray {
            let queryArriArray = queryStr.components(separatedBy: "=")
            if queryArriArray.count == 2
            {
                let key = queryArriArray[0]
                let value = queryArriArray[1].removingPercentEncoding
                dic[key] = value
            }
        }
        return dic
    }
}
